using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;

namespace RayDen.Library.Components.Color
{
    public class FloatRadiance : RadianceEvaluationBase<float>
    {
        public FloatRadiance(ref float spc)
            : base(ref spc)
        {
        }

        public FloatRadiance(float spc)
            : base(spc)
        {
        }
        public FloatRadiance(FloatRadiance spc)
            : base(spc)
        {
        }

        public override IRadianceEvaluation Mul(float value)
        {
            this.Spectra *= value;
            return this;
        }

        public override IRadianceEvaluation Div(float value)
        {
            this.Spectra /= value;
            return this;
        }

        public override IRadianceEvaluation Add(float value)
        {
            this.Spectra += value;
            return this;
        }

        public override void ToRgbSpectrum(out RgbSpectrum result)
        {
            result = new RgbSpectrum(Spectra);
        }

        public override bool IsBlack
        {
            get
            {
                return Spectra.NearZero();
            }
            protected set
            {

            }
        }

        public override float[] ToArray()
        {
            return new[] { Spectra };
        }

        protected override RadianceEvaluationBase<float> Multiply(RadianceEvaluationBase<float> val)
        {
            this.Spectra *= val.Spectra;
            return this;
        }

        protected override RadianceEvaluationBase<float> Add(RadianceEvaluationBase<float> val)
        {
            this.Spectra += val.Spectra;
            return this;
        }

        protected override IRadianceEvaluation Convert(IRadianceEvaluation val)
        {
            if (val is RgbRadiance)
            {
                var value = (val as RgbRadiance);
                return new FloatRadiance(value.Spectra.Average);
            }
            else if (val is SpectralRadiance)
            {
                var value = (val as SpectralRadiance);
                return new FloatRadiance(value.Spectra.ToRgb().Average);
            }
            return val;
        }

        public override IRadianceEvaluation Clone()
        {
            return new FloatRadiance(this);
        }


        
    }
}